package com.jrx.anytxn.transaction.job.customer.step;

import com.alibaba.fastjson.JSONObject;
import com.jrx.anytxn.customer.entity.CmCustomerExtInfo;
import com.jrx.anytxn.customer.service.ICustomerLimitInfoService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.service.product.IProductTableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @comment: 产品激活 Processor
 * @author: liuzb
 * @date: 2023/05/03 10:00
 */
@Service
public class ActiveProductProcessor implements ItemProcessor<CmCustomerExtInfo, CmCustomerExtInfo> {

    private static final Logger logger = LoggerFactory.getLogger(ActiveProductProcessor.class);

    @Autowired
    IProductTableService productTableService;
    @Autowired
    private ICustomerLimitInfoService iCustomerLimitInfoService;

    @Override
    public CmCustomerExtInfo process(CmCustomerExtInfo extUser) throws Exception {
        CmCustomerExtInfo updateCus = new CmCustomerExtInfo();
        String tenantId = extUser.getTenantId();
        // 上线需要readme配合手动执行
        logger.info("用户信息：{}", JSONObject.toJSONString(extUser));
        List<String> activeProducts = iCustomerLimitInfoService.getActiveProducts(extUser.getCustomerId(), tenantId, Collections.singletonList(extUser.getProductId()));
        if (activeProducts == null) {
            return null;
        }
        updateCus.setId(extUser.getId());
        updateCus.setTenantId(tenantId);
        updateCus.setUpdateBy(Constant.SYS_OPRATE);
        updateCus.setRecordVersionNumber(extUser.getRecordVersionNumber() + 1);
        updateCus.setProductActiveFlag(Constant.YES_FLAG);
        return updateCus;
    }

}
